package com.carl.common;




import com.carl.util.DateTimeUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class ReturnMapUtil {

    private ReturnMapUtil() {

    }

    public static final String REQUEST_TIME = "requestTime";

    public static final String CODE = "code";
    public static final String SUCCESS = "success";

    public static final String MESSAGE = "message";

    public static final String MSG_ID = "msgId";

    public static final String HEADER = "header";

    public static final String BODY = "body";
    public static final String DUPLICATE_ENTRY = "Duplicate entry";

    public static final Random RANDOM = new Random();

    /**
     * 返回成功
     *
     * @return
     */
    public static Map<String, Object> getSuccessMap() {
        Map<String, Object> successMap = new HashMap<>();
        successMap.put(SUCCESS, true);
        successMap.put(MESSAGE, "操作成功");
        return successMap;
    }


    public static Map<String, Object> serviceDataMap(Object o) {
        Map<String, Object> serviceDataMap = new HashMap<>();
        serviceDataMap.put("data", o);
        return serviceDataMap;
    }

    public static Map<String, Object> serviceDataMapNoData() {
        Map<String, Object> serviceDataMap = new HashMap<>();
        serviceDataMap.put("data", new HashMap<>());
        return serviceDataMap;
    }


    /**
     * 返回成功
     *
     * @return
     */
    public static Map<String, Object> getSuccessMap(Map<String, Object> body) {
        Map<String, Object> successMap = new HashMap<>();
        Map<String, String> header = new HashMap<>();
        header.put(REQUEST_TIME, DateTimeUtil.formateDateStr(new Date(), DateTimeUtil.YYYY_MM_DDHHMMSS));
        header.put(CODE, ReturnCodeConstant.SUCCES_CODE);
        header.put(MESSAGE, "操作成功");
//        header.put(MSG_ID, msgId);//从调用方获取后赋值
        successMap.put(HEADER, header);
        successMap.put(BODY, body);//放入响应信息
        return successMap;
    }


    /**
     * 返回成功
     *
     * @return
     */
    public static <T> Map<String, Object> getSuccessMap(T body) {
        Map<String, Object> successMap = new HashMap<>();
        Map<String, String> header = new HashMap<>();
        header.put(REQUEST_TIME, DateTimeUtil.formateDateStr(new Date(), DateTimeUtil.YYYY_MM_DDHHMMSS));
        header.put(CODE, ReturnCodeConstant.SUCCES_CODE);
        header.put(MESSAGE, "操作成功");
        //        header.put(MSG_ID, msgId);//从调用方获取后赋值
        successMap.put(HEADER, header);
        successMap.put(BODY, body);//放入响应信息
        return successMap;
    }



    /**
     * 返回异常
     *
     * @return
     */
    public static Map<String, Object> getErrorMap() {
        Map<String, Object> errorMap = new HashMap<>();
        Map<String, String> header = new HashMap<>();
        header.put(REQUEST_TIME, DateTimeUtil.formateDateStr(new Date(), DateTimeUtil.YYYY_MM_DDHHMMSS));
        //默认未知的错误，后续替换为具体的错误码
        header.put(CODE, ReturnCodeConstant.CODE_99999);
        header.put(MESSAGE, "未知的错误");
        header.put(MSG_ID, "");//从调用方获取后赋值
        errorMap.put(HEADER, header);
        Map<String, Object> body = new HashMap<>();
        errorMap.put(BODY, body);//放入响应信息
        return errorMap;
    }
    public static Map<String, Object> getErrorMap(String message) {
        Map<String, Object> errorMap = new HashMap<>();
        errorMap.put(SUCCESS, false);
        errorMap.put(CODE, false);
        errorMap.put(MESSAGE, "操作成功");
        return errorMap;
    }


}